import 'dart:async';

import 'package:flutter/material.dart';
import 'package:shopping_group/base/base_constant.dart';
import 'package:shopping_group/entity/widget_action_entity.dart';
import 'package:shopping_group/event/event.dart';
import 'package:shopping_group/modle/person/account/view/lz_mine_account_detail_page.dart';
import 'package:shopping_group/modle/person/bloc/my_money_bloc.dart';
import 'package:shopping_group/modle/play/entity/user_account_entity.dart';
import 'package:shopping_group/res/colors.dart';
import 'package:shopping_group/res/dimens.dart';
import 'package:shopping_group/utils/route_util.dart';
import 'package:shopping_group/utils/widget_utils.dart';

import 'balance_money_page.dart';
import 'bank_list_page.dart';
import 'cash_money_page.dart';

class ChangeViewMoneyPage extends StatefulWidget {
  @override
  _ChangeViewMoneyPageState createState() => _ChangeViewMoneyPageState();
}

class _ChangeViewMoneyPageState extends State<ChangeViewMoneyPage> {
  List<Widget> listViews = [
    BalanceMoneyPage(),
    CashMoneyPage(),
  ];

  List<String> listStr = [
    '余额明细',
    '提现记录',
  ];
  int _index = 0;
  List<WidgetActionEntity> _actionList = [];

  UserAccountEntity _entity = UserAccountEntity();
  MyMoneyBloc _myMoneyBloc;
  StreamSubscription _refresSubscription;
  StreamSubscription _jsSubscription;
  @override
  void initState() {
    super.initState();
    _myMoneyBloc = MyMoneyBloc();
    _myMoneyBloc.initDate.add(null);
    _actionList
      // ..add(WidgetActionEntity()
      //   ..actionType = 'text'
      //   ..title = BaseConstant.hadFreeGmName
      //   ..iconData = Icons.menu
      //   ..titleSize = Dimens.FONT_SIZE_14
      //   ..actionColor = BaseConstant.hadFreeGmName.contains('已解锁')
      //       ? Colours.gray_91
      //       : Colours.title_white)
      ..add(WidgetActionEntity()
        ..actionType = 'menu'
        ..title = '提现'
        // ..iconData = Icons.menu
        ..titleSize = Dimens.FONT_SIZE_14
        ..actionColor = Colours.title_white);

    _jsSubscription = eventBus.on<JiesuoOkEvent>().listen((event) {
      setState(() {
        BaseConstant.hadFreeGmName = '已解锁';
        _actionList[0]
          ..actionColor = Colours.gray_91
          ..title = '已解锁';
      });
    });

    _refresSubscription = eventBus.on<RefreshEvent>().listen((event) {
      _myMoneyBloc.initDate.add(null);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: WidgetUtils.showAppar(context, '我的账户',
          actionList: _actionList,
          textColor: Colors.white,
          isHide: false,
          backgroundColor: Colours.blackground_609, onTapCall: (entity) {
        if (entity is WidgetActionEntity) {
          if ('text' == entity.actionType) {
            if ('已解锁' != BaseConstant.hadFreeGmName) {
              _jiesuoOut();
            }
          } else if ('menu' == entity.actionType) {
            // ShowBottomSheet.showTxView(context);
            RouteUtil.goParameter(BankListPage(txType: '电商提现'));
          }
        }
      }),
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          // 触摸收起键盘
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: SafeArea(
          child: Container(
            decoration: BoxDecoration(
              color: Colours.title_white,
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Expanded(
                    flex: 1,
                    child: Container(
                      decoration: BoxDecoration(
                        color: Colours.blackground_609,
                      ),
                      child: Stack(
                        children: <Widget>[
                          StreamBuilder(
                              stream: _myMoneyBloc.sendAllData,
                              initialData: _entity,
                              builder: (BuildContext contexts, AsyncSnapshot<Object> snapshot) {
                                Object object = snapshot.data;
                                if (object is UserAccountEntity) {
                                  _entity = object;
                                }
                                return Container(
                                  alignment: Alignment.centerLeft,
                                  padding: EdgeInsets.symmetric(horizontal: 16, vertical: 4),
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      Row(
                                        crossAxisAlignment: CrossAxisAlignment.center,
                                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                        children: [
                                          WidgetUtils.getTextColorLineView(
                                              '总余额(元)', Colours.title_white, Dimens.FONT_SIZE_16),
                                          // GestureDetector(
                                          //   onTap: () {
                                          //     RouteUtil.goParameter(
                                          //         LzMineAccountDetailPage(accountStyle: LzAccountStyle.myAdverRights));
                                          //   },
                                          //   child: WidgetUtils.getTextColorLineView(
                                          //       '看广告权益:${_entity.dsGgNoDoCt ?? "0"}',
                                          //       Colours.title_white,
                                          //       Dimens.FONT_SIZE_16),
                                          // )
                                        ],
                                      ),
                                      _moneyView(),
                                    ],
                                  ),
                                );
                              }),
                        ],
                      ),
                    )),
                SizedBox(
                  height: 8,
                ),
                Expanded(
                  child: bodyView(),
                  flex: 3,
                ),
                SizedBox(
                  height: 8,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Future _jiesuoOut() async {
    showDialog(
        context: context,
        builder: (context) {
          return showAlertDialog();
        }).then((var result) {
      if (null == result) {
        return;
      }
      if (result is String && 'yes' == result) {
        _myMoneyBloc.jsDate.add(null);
      }
    });
  }

  Widget showAlertDialog() {
    return AlertDialog(
      title: WidgetUtils.getTextColorView('选择', Colors.black, Dimens.FONT_SIZE_18),
      content: WidgetUtils.getTextColorView('解锁后将不可参与任何推广活动，真的要解锁吗？', Colors.black, Dimens.FONT_SIZE_16),
      actions: <Widget>[
        FlatButton(
          onPressed: () {
            Navigator.of(context).pop('yes');
          },
          child: WidgetUtils.getTextColorView('确定', Colors.black, Dimens.FONT_SIZE_16),
        ),
        FlatButton(
            onPressed: () {
              Navigator.of(context).pop();
            },
            child: WidgetUtils.getTextColorView('取消', Colors.black, Dimens.FONT_SIZE_16))
      ],
    );
  }

  Widget _moneyView() {
    return Expanded(
        child: Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        Expanded(
          child: Container(
            alignment: Alignment.center,
            child: WidgetUtils.getTextColorView(
              _entity.dsAllAcct,
              Colours.title_white,
              Dimens.FONT_SIZE_30,
            ),
          ),
        ),
        Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  GestureDetector(
                    onTap: () {
                      RouteUtil.goParameter(LzMineAccountDetailPage(accountStyle: LzAccountStyle.myJintie));
                    },
                    child: Row(
                      children: [
                        WidgetUtils.getTextColorView(
                          '津贴',
                          Colours.title_white,
                          Dimens.FONT_SIZE_14,
                        ),
                        WidgetUtils.getTextColorView(
                          '(${_entity.jinTieAcct ?? ""})',
                          Colours.title_white,
                          Dimens.FONT_SIZE_14,
                        ),
                      ],
                    ),
                  ),

                  GestureDetector(
                    onTap: () {
                      RouteUtil.goParameter(LzMineAccountDetailPage(accountStyle: LzAccountStyle.myDJQ));
                    },
                    child: Row(
                      children: [
                        WidgetUtils.getTextColorView(
                          '代金券',
                          Colours.title_white,
                          Dimens.FONT_SIZE_14,
                        ),
                        WidgetUtils.getTextColorView(
                          '(${_entity.djqAcct ?? ""})',
                          Colours.title_white,
                          Dimens.FONT_SIZE_14,
                        ),
                      ],
                    ),
                  )
                  // Expanded(child: Container()),
                  // GestureDetector(
                  //   onTap: () {
                  //     RouteUtil.goParameter(SelectMoneyJuanPage());
                  //   },
                  //   child: Container(
                  //     padding: EdgeInsets.all(4),
                  //     alignment: Alignment.center,
                  //     color: Colours.transparent,
                  //     child: Row(
                  //       children: <Widget>[
                  //         Icon(
                  //           Icons.attach_money,
                  //           color: Colours.title_white,
                  //           size: 16,
                  //         ),
                  //         WidgetUtils.getTextColorView(
                  //           '',
                  //           Colours.title_white,
                  //           Dimens.FONT_SIZE_14,
                  //         ),
                  //       ],
                  //     ),
                  //   ),
                  // ),
                ],
              ),
            ),
            SizedBox(height: 10),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                // Container(
                //   child: GestureDetector(
                //     onTap: () {
                //       // RouteUtil.goParameter(TgfMoneyPage());
                //       RouteUtil.goParameter(LzMineAccountDetailPage(accountStyle: LzAccountStyle.myZt));
                //     },
                //     child: Row(
                //       children: [
                //         WidgetUtils.getTextColorView(
                //           '直推奖',
                //           Colours.title_white,
                //           Dimens.FONT_SIZE_14,
                //         ),
                //         WidgetUtils.getTextColorView(
                //           '(${_entity.dsZtAcct ?? ""})',
                //           Colours.title_white,
                //           Dimens.FONT_SIZE_14,
                //         ),
                //       ],
                //     ),
                //   ),
                // ),
                // Container(
                //   child: GestureDetector(
                //     onTap: () {
                //       // RouteUtil.goParameter(TgfMoneyPage());
                //       RouteUtil.goParameter(LzMineAccountDetailPage(accountStyle: LzAccountStyle.myProductShelf));
                //     },
                //     child: Row(
                //       children: [
                //         WidgetUtils.getTextColorView(
                //           '商品上架权益',
                //           Colours.title_white,
                //           Dimens.FONT_SIZE_14,
                //         ),
                //         WidgetUtils.getTextColorView(
                //           '(${_entity.canUpMycommoCt ?? ""})',
                //           Colours.title_white,
                //           Dimens.FONT_SIZE_14,
                //         ),
                //       ],
                //     ),
                //   ),
                // ),

                // Expanded(child: Container()),
                // GestureDetector(
                //   onTap: () {
                //     RouteUtil.goParameter(MoreMoneyJuanPage(
                //       byYonJin: 'Y',
                //     ));
                //   },
                //   child: Container(
                //     alignment: Alignment.center,
                //     padding: EdgeInsets.all(4),
                //     color: Colours.transparent,
                //     child: Row(
                //       children: <Widget>[
                //         Icon(
                //           Icons.attach_money,
                //           color: Colours.title_white,
                //           size: 16,
                //         ),
                //         WidgetUtils.getTextColorView(
                //           '转代理券',
                //           Colours.title_white,
                //           Dimens.FONT_SIZE_14,
                //         ),
                //       ],
                //     ),
                //   ),
                // ),
              ],
            ),
            // SizedBox(height: 10),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Container(
                  child: GestureDetector(
                    onTap: () {
                      // RouteUtil.goParameter(TgfMoneyPage());
                      RouteUtil.goParameter(LzMineAccountDetailPage(accountStyle: LzAccountStyle.myDsFuli));
                    },
                    child: Row(
                      children: [
                        WidgetUtils.getTextColorView(
                          '福利金',
                          Colours.title_white,
                          Dimens.FONT_SIZE_14,
                        ),
                        WidgetUtils.getTextColorView(
                          '(${_entity.dsFuliAcct ?? ""})',
                          Colours.title_white,
                          Dimens.FONT_SIZE_14,
                        ),
                      ],
                    ),
                  ),
                ),
                GestureDetector(
                  onTap: () {
                    RouteUtil.goParameter(LzMineAccountDetailPage(accountStyle: LzAccountStyle.myAdverRights));
                  },
                  child: WidgetUtils.getTextColorLineView(
                      '看广告权益:${_entity.dsGgNoDoCt ?? "0"}', Colours.title_white, Dimens.FONT_SIZE_16),
                )
                // num.parse(_entity.groupJtAcct ?? "0") < 0
                //     ? Container()
                //     : GestureDetector(
                //         onTap: () {
                //           // RouteUtil.goParameter(TgfMoneyPage());
                //           RouteUtil.goParameter(LzMineAccountDetailPage(accountStyle: LzAccountStyle.myTeamJT));
                //         },
                //         child: Container(
                //           child: Row(
                //             children: [
                //               WidgetUtils.getTextColorView(
                //                 '团队津贴盈余',
                //                 Colours.title_white,
                //                 Dimens.FONT_SIZE_14,
                //               ),
                //               WidgetUtils.getTextColorView(
                //                 '(${_entity.canUpMycommoCt ?? ""})',
                //                 Colours.title_white,
                //                 Dimens.FONT_SIZE_14,
                //               ),
                //             ],
                //           ),
                //         ),
                //       ),
              ],
            ),
          ],
        ),
      ],
    ));
  }

  Widget bodyView() {
    return Container(
        color: Colours.transparent,
        padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        child: Flex(
          direction: Axis.vertical,
          children: <Widget>[
            changeBottomView(),
            Expanded(
              child: listViews[_index],
            ),
          ],
        ));
  }

  Widget changeBottomView() {
    return Container(
      color: Colors.white,
      alignment: Alignment.center,
      padding: const EdgeInsets.only(top: 12),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Expanded(
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                showChangeView(
                  0,
                  listStr[0],
                  _index == 0 ? Colours.blackground_609 : Colours.text_2222,
                  _index == 0 ? Colours.blackground_609 : Colors.transparent,
                ),
                showChangeView(
                  1,
                  listStr[1],
                  _index == 1 ? Colours.blackground_609 : Colours.text_2222,
                  _index == 1 ? Colours.blackground_609 : Colors.transparent,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget showChangeView(int index, String title, Color color, Color dividerColor) {
    return Expanded(
      child: GestureDetector(
        onTap: () {
          setState(() {
            _index = index;
          });
        },
        child: Container(
          alignment: Alignment.center,
          color: Colors.transparent,
          child: Stack(
            children: <Widget>[
              Column(
                children: <Widget>[
                  WidgetUtils.getTextColorView(title, color, Dimens.FONT_SIZE_14),
                  Container(
                    padding: const EdgeInsets.only(top: 12),
                    //margin: EdgeInsets.symmetric(horizontal: 56),
                    width: 64,
                    child: WidgetUtils.drawCodeDivider(color: dividerColor, height: 2),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    _refresSubscription?.cancel();
    _jsSubscription?.cancel();
    super.dispose();
  }
}
